#include <behaviortree_cpp_v3/bt_factory.h>
#include <iostream>
using namespace std;

class SaySomething : public BT::SyncActionNode
{
public:
    SaySomething(const std::string& name, const BT::NodeConfiguration& config)
        : BT::SyncActionNode(name, config)
    {
    }

    // You must override the virtual function tick()
    BT::NodeStatus tick() override
    {
        std::string message;
        getInput("message", message);
        std::cout << this->name() << " message: " << message << std::endl;
        return BT::NodeStatus::SUCCESS;
    }

    // It is mandatory to define this static method.
    static BT::PortsList providedPorts()
    {
        return{ BT::InputPort<std::string>("message") };
    }
};

// 带参数的节点, 输入变量为message, 全局变量
static const char* xml_text = R"(
 <root main_tree_to_execute = "MainTree" >

     <BehaviorTree ID="MainTree">
        <Sequence name="root">
            <SaySomething message="hi, I am test input port" />
        </Sequence>
     </BehaviorTree>

 </root>
 )";

int main()
{
    BT::BehaviorTreeFactory factory;

    // 注册节点类型
    factory.registerNodeType<SaySomething>("SaySomething");

    // 从xml 加载行为树
    BT::Tree tree = factory.createTreeFromText(xml_text);

    // 打印行为树结构到标准输出
    cout << "=== 行为树结构 ===" << endl;
    printTreeRecursively(tree.rootNode());

    // 执行行为树
    tree.tickRootWhileRunning();

    return 0;
}

/*当 <SaySomething message="abcdefg123456" />
SaySomething message: abcdefg123456

*/

/*当 <SaySomething message="hi, I am test input port"
SaySomething message: hi, I am test input port

*/